package val.to.entities;

import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import val.to.server.JsonUtils;
import android.os.Parcel;
import android.os.Parcelable;

public class FeedItem implements Parcelable{
	private int m_id;
	private String m_comment;
	private String m_image;
	private String m_smallphoto;
	private String m_thumbnailphoto;
	private String m_userId;
	private String m_userName;
	private Date m_timestamp;
	private long m_mscAgo;
	private long m_createdAt;
	private boolean m_editable;

	private FeedItem(int id,
			String userId,
			String username, 
			String comment,
			String image, 
			String smallphoto, 
			String thumbnailPhoto,
			Date timestamp, 
			long mscAgo,
			boolean editable){
		m_id = id;
		m_userId = userId;
		m_userName = username;
		m_comment = comment;
		m_image = image;
		m_smallphoto = smallphoto;
		m_thumbnailphoto = thumbnailPhoto;
		m_timestamp = timestamp;
		m_mscAgo = mscAgo;
		m_createdAt = System.currentTimeMillis();
		m_editable = editable;
	}
	
	public int getId() { return m_id; }
	public String getComment() { return m_comment; }
	public String getUserId() { return m_userId; }	
	public String getUsername() { return m_userName; }
	public Date getTimestamp() { return m_timestamp; }
	public String getImageUri() { return m_image; }
	public String getSmallPhotoUri() { return m_smallphoto; }
	public String getThumbnailPhotoUri() { return m_thumbnailphoto; }
	public boolean getEditable() { return m_editable; }
	public long getMscAgo() { 
		return m_mscAgo + System.currentTimeMillis() - m_createdAt; 
	}
	
	private FeedItem(Parcel in){
		m_id = in.readInt();
		m_mscAgo = in.readLong();
		m_userId = in.readString();
		m_comment = in.readString();
		m_userName = in.readString();
		m_image = in.readString();		
		m_smallphoto = in.readString();	
		m_thumbnailphoto = in.readString();
		long time = in.readLong();
		m_editable = in.readInt() == 1;
		m_timestamp = new Date(time);
	}
	
	@Override
	public String toString() {
		return String.format("[FeedItem %d] %s", m_id, m_comment);
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null) return false;
		if (this == o) return true;
		if (!(o instanceof FeedItem)) return false;
		
		return ((FeedItem)o).getId() == this.getId();
	}
	
	public static FeedItem FromJson(JSONObject json) throws JSONException{
		int id = json.getInt("id");
		long msc = (long)json.getDouble("millisecondsDiff");
		String comment = json.getString("comment");
		String userId = json.getString("fbuserid");
		String username = json.getString("name");
		String photo = json.getString("smallPhoto"); //mobile Valto uses small photo	
		String thumbnail = json.getString("thumbnail");		
		String smallphoto = json.getString("smallPhoto"); //mobile Valto uses small photo	
		String strTS = json.getString("date");	
		boolean editable = json.getBoolean("editable");	
		
		return new FeedItem(id,
				userId,
				username, 
				comment, 
				photo, 
				smallphoto, 
				thumbnail,
				JsonUtils.dateFromJson(strTS), 
				msc, 
				editable);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(m_id);
		dest.writeLong(m_mscAgo);
		dest.writeString(m_userId);
		dest.writeString(m_comment);
		dest.writeString(m_userName);
		dest.writeString(m_image);
		dest.writeString(m_smallphoto);
		dest.writeString(m_thumbnailphoto);
		dest.writeLong(m_timestamp.getTime());
		dest.writeInt(m_editable ? 1 : 0);
	}	
	
    public static final Parcelable.Creator<FeedItem> CREATOR = new Parcelable.Creator<FeedItem>() {
		public FeedItem createFromParcel(Parcel in) {
		    return new FeedItem(in);
		}
		
		public FeedItem[] newArray(int size) {
		    return new FeedItem[size];
		}
	};
}
